<?php
class ProfessionalHome
{
    function getCountries()
    {
        /* :TODO: Separate database and view */
        global $db;
        global $html;
        $sql = 'select ID, Name from Country order by Name ASC';
        $result = $db->query($sql);
        $countries = '';
        $n = $db->rowCount($result);
        for ($i = 0; $i < $n; $i++) {
            $row = $db->fetch($result);
            $selected = '';
            if(empty($_POST['country'])){$_POST['country'] = '412';}
            if($row['ID'] === $_POST['country']) {
                $selected = ' selected="selected"';
            }
            $countries .= '<option'.$selected.' value="'.$row['ID'].'">'.$html->text($row['Name']).'</option>';
        }
    }
    function getPersonCount()
    {
        global $db;
        $sql = "select count(*) from prosearchperson";
        $result = $db->query($sql);
        $totalNames = (float) mysql_result($result, 0);
        $totalNamesNum = number_format($totalNames, 0, '', ',');
    }
    function showProfessionalBox($totalNamesNum, $countries)
    {
        return <<< EOT
  <li class="professionalbox">
    <h2 style="text-align:right;">
        <span style="float:left;">Professionals searching for...</span>
        <span style="font-size:14px;"><strong style="font-family: Verdana, Helvetica, Arial, sans-serif;font-size:12px;">$totalNamesNum</strong> people</span>
    </h2>
    <p><a href="professionalmore.php">Is a lawyer looking for you?</a></p>
    <form action="professionalsearch.php" method="get" onsubmit="return verifyprofessional(this)" style="text-align:right;">
        <p>
            <label>*</label>
            <input name="firstname" value="Your First Name"  onfocus="removeText(this)" onblur="resetText(this)" />
        </p>
        <p>
            <label>*</label>
            <input name="surname" value="Your Last Name"  onfocus="removeText(this)" onblur="resetText(this)" />
        </p>
        <p>
            <label>*</label>
            <select name="country">
                <option value="not">Country</option>
                $countries
            </select>
        </p>
        <p>
            <label class="reqnote">* required</label>
            <input type="submit" class="gobutton" value="go"/>
        </p>
    </form>
  </li>
EOT;
    }
    function render()
    {
        $countries = $this->getCountries();
        $totalNamesNum = $this->getPersonCount();
        return $this->showProfessionalBox($totalNamesNum, $countries);
    }
}
?>
